/*
 * @Description: 
 * @version: 
 * @Author: hui.wang01
 * @Date: 2020-12-23 12:47:08
 * @LastEditors: Please set LastEditors
 */
const myPromise = require('./myPromisefinally')
let promise = new myPromise((resolve,reject) => {
  // resolve('成宫')
  reject('失败')

  // 异步
  // setTimeout(()=>{
  //   // resolve('成功*********')
  //   reject('shibai**')
  // },2000)
})

// then方法多次调用添加多个处理函数
promise.then((value) => {
  console.log('1')
  console.log(value)
} , (reason) => {
  console.log('1失败')
  console.log(reason)
})

promise.then((value) => {
  console.log('2')
  console.log(value)
} , (reason) => {
  console.log('2失败')
  console.log(reason)
})

promise.then((value) => {
  console.log('3')
  console.log(value)
} , (reason) => {
  console.log('3失败')
  console.log(reason)
})

// then方法是可以被链式调用的, 后面then方法的回调函数拿到值的是上一个then方法的回调函数的返回值
function other () {
  return 'other'
}
promise.then(res=>{
  console.log(res)
  return 100
}).then(res=>{
  console.log(res)
  return other()
}).then(res => {
  console.log(res)
})

// then方法链式调用识别Promise对象自返回
//  自己返回自己就会形成死循环？,会报错
// let p1 = promise.then(res=>{
//     console.log(res)
//     return p1
//   })
//   p1.then(res=>{
//     console.log(res)    
//   },(reason)=>{
//     console.log('reason:',reason.message) // tips: 错误原因:reason.message
//   })

//  1.executor执行报错 2.回调报错
// let promise = new myPromise((resolve,reject) => {
//   // throw new Error('executor error')
//   // resolve('成宫')
//   // reject('失败')


//   // 异步
//   setTimeout(()=>{
//     resolve('成功*********')
//     // reject('shibai**')
//   },2000)
// }).then(res=>{
//   // throw new Error('then resolve回调报错')
//   console.log(res)
//   return 'aaa'
// }, reason => {
//   console.log('reason******')
//   console.log(reason.message)
//   throw new Error('then reject回调报错')
// }).then(res=>{
//   console.log(res)
// },reject => {
//   console.log('then 回调报错****')
//   console.log(reject.message)
// })

// then回调可选
// let promise = new myPromise((resolve,reject) => {
//   // throw new Error('executor error')
//   resolve('成宫')
//   // reject('失败')
// })

// promise.then().then(value => {console.log(value)},(reason)=>{
//   console.log(reason)
// })

// Promise.all
// let p1 = () => new myPromise((resolve,reject) => {
//   // throw new Error('executor error')
//   resolve('成宫')
//   // reject('失败')
// })

// let p2 = () => new myPromise((resolve,reject) => {
//     // 异步
//     setTimeout(()=>{
//       // resolve('成功*********')
//       reject('shibai**')
//     },2000)
// })
// let p3 = () => new myPromise((resolve,reject) => {
//   throw new Error('executor error')
// })

// myPromise.all(['array1', 'array2', p1(), p2(), 'array3'])
// .then(res => {console.log(res)},(reason)=>{
//   console.log(reason)
// })

// Promise.resolve
// myPromise.resolve('resolved***').then((value) => {
//   console.log(value)
// },(reason) => {
//   console.log(reason)
// })

// let p1 = new myPromise((resolve,reject) => {
//   // throw new Error('executor error')
//   // resolve('成宫')
//   reject('失败')
// })
// myPromise.resolve(p1).then((value) => {
//   console.log(value)
// },(reason) => {
//   console.log(reason)
// })

// Promise finally

let p1 = new myPromise((resolve,reject) => {
  // throw new Error('executor error')
  resolve('成宫')
  // reject('失败')
})
// p1.finally(res => {
//   console.log(res)
// }).then(value=>console.log('成功啦',value), reason => {console.log('shibai',reason)})